package ${package_controller};
import ${package_pojo}.${Table};
import ${package_service}.${Table}Service;
import com.github.pagehelper.PageInfo;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
<#if swagger==true>import io.swagger.annotations.*;</#if>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author : 残影
 * @Date: 2020/7/29 13:44
 */
@RestController
@CrossOrigin
@RequestMapping("/${table}")
<#if swagger==true>@Api(tags = "${Table}Controller")</#if>
public class ${Table}Controller {
    @Autowired
    private ${Table}Service ${table}Service;

    <#if swagger==true>
    @ApiOperation("查询全部数据")
    </#if>
    @GetMapping
    public Result<${Table}> findAll(){
        List<${Table}> list = ${table}Service.findAll();
        return new Result(true, StatusCode.OK,"查询成功",list) ;
    }

    <#if swagger==true>
    @ApiOperation("根据ID查询数据")
    </#if>
    @GetMapping("/{id}")
    public Result<${Table}> findById(@PathVariable ${keyType} id){
        ${Table} ${table} = ${table}Service.findById(id);
        return new Result<${Table}>(true,StatusCode.OK,"查询成功",${table});
    }

    <#if swagger==true>
    @ApiOperation("新增数据")
    </#if>
    @PostMapping
    public Result add(@RequestBody ${Table} ${table}){
        ${table}Service.add(${table});
        return new Result(true,StatusCode.OK,"添加成功");
    }

    <#if swagger==true>
    @ApiOperation("修改数据")
    </#if>
    @PutMapping(value="/{id}")
    public Result update(@RequestBody <#if swagger==true>@ApiParam(name = "${Table}对象",value = "传入JSON数据",required = false)</#if> ${Table} ${table},@PathVariable ${keyType} id){
        //设置主键值
        ${table}.${keySetMethod}(id);
        ${table}Service.update(${table});
        return new Result(true,StatusCode.OK,"修改成功");
    }

    <#if swagger==true>
    @ApiOperation("根据ID删除数据")
    </#if>
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable ${keyType} id){
        ${table}Service.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    <#if swagger==true>
    @ApiOperation("条件分页查询")
    </#if>
    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody(required = false) <#if swagger==true>@ApiParam(name = "${Table}对象",value = "传入JSON数据",required = false)</#if> ${Table} ${table}, @PathVariable  int page, @PathVariable  int size){
        PageInfo<${Table}> pageInfo = ${table}Service.findPage(${table}, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    <#if swagger==true>
    @ApiOperation("分页搜索实现")
    </#if>
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        PageInfo<${Table}> pageInfo = ${table}Service.findPage(page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    <#if swagger==true>
    @ApiOperation("多条件搜索品牌数据")
    </#if>
    @PostMapping(value = "/search" )
    public Result findList(@RequestBody(required = false) <#if swagger==true>@ApiParam(name = "${Table}对象",value = "传入JSON数据",required = false)</#if> ${Table} ${table}){
        List<${Table}> list = ${table}Service.findList(${table});
        return new Result(true,StatusCode.OK,"查询成功",list);
    }
}
